/**
 * Created by dylan_Apple on 17/6/25.
 */
var async = require('async');
var stockChannelDAO = require('../controller/controller.stockChannel');
var knowledgeGroupDAO = require('../controller/controller.knowledgeGroup');
var user = require('../controller/controller.user');
var stockUtil = require('../utils/stock');
module.exports = {

    //获取知识库分组
    getStockRecommendListWithGroup:function(req,res,next){
        var that = this;
        async.parallel({
            knowledgeGroup:function(done){
                that.listStockPoolGroup(req,res,function(result){
                    if(result.obj)
                        done(null,result.obj);
                    else
                        done(result,null);
                });
            },
            stockRecommendList:function(done){
                req.query.isRecommend = "0";
                that.getStockRecommendList(req,res,function(result){
                     done(null, result);
                });
            }
        },function(error,result){
            if(!error){
                var stockCodes = [];
                var list = result&&result.stockRecommendList&&result.stockRecommendList.mconferencelist||[];
                for(stock of list){
                    stockCodes.push(stock.stockId && stock.stockId.stockCode || "");
                }
                stockUtil.getRealtimeListPrice(stockCodes,function (err,res) {
                    next(createStockPoolTree(result.stockpoolGroup, result.stockRecommendList,res));
                });


            }
            else{
                console.error(error);
                res.redirect('/error');
                return;
            }
        });

        function createStockPoolTree(stockpoolGroup, stockRecommendListObj,prices) {
            var stockRecommendList = stockRecommendListObj.mconferencelist;
            var tree = stockpoolGroup,rootRecommendList = [],i;
            var flatMap = {};
            for (i=0;i<stockpoolGroup.length;i++){//初始化flatMap
                var j;
                var parent = stockpoolGroup[i];
                var key = parent.id;
                parent.list = [];
                flatMap[key] = parent;

                var children = parent.children || [];
                for (j = 0;j<children.length;j++){
                   var child = children[j];
                   var key = child.id;
                   child.list = [];
                   flatMap[key] = child;
                }
            }

            for (i=0;i<stockRecommendList.length;i++){//为flatMap赋值
                stockRecommendList[i].realPrice = prices[i];
                var stock = stockRecommendList[i];
                var parentPoolId = stock.parentPoolId;
                var childPoolId = stock.childPoolId;
                if (childPoolId && flatMap[childPoolId]){
                    flatMap[childPoolId].list.push(stock);
                    flatMap[parentPoolId].count = (flatMap[parentPoolId].count || 0)+1;
                }else if (parentPoolId && flatMap[parentPoolId]){
                    flatMap[parentPoolId].list.push(stock);
                }else{
                    rootRecommendList.push(stock);
                }
            }

            for (i=0;i<tree.length;i++){//整理tree
                var j,parentCount;
                var parent = tree[i];
                var key = parent.id;
                parentCount = parent.list.length;
                var children = parent.children || [];
                for (j = 0;j<children.length;j++){
                    var child = children[j];
                    var key = child.id;
                    child.count = flatMap[key].list.length;
                    parentCount += child.count;
                }
                parent.count = parentCount;
                parentCount=0;
            }

            return {mconferencelist:stockRecommendListObj.mconferencelist,tree:tree, other:rootRecommendList,moment:stockRecommendListObj.moment};
        }
    },
    //新建股票池分组
    createKnowledgeGroup:function(req, res, next){
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        knowledgeGroupDAO.create(params,function(err,result){
            next(err || result);
        });
    },
    //修改股票池分组名称/删除股票池分组
    updateKnowledgeGroup:function(req, res, next){
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        knowledgeGroupDAO.update(params,function(err,result){
            next(err || result);
        });
    },
    //查询股票池分组列表
    listKnowledgeGroup:function(req,res,next){
        var params = req.body;
        if (params == null) {
            res.redirect('/error');
            return;
        }
        async.waterfall([
            function(cb){
                knowledgeGroupDAO.list(params,function(err,result){
                    console.log(result);
                    cb(err, result);
                });
            },
            function(result,cb){
                var list = result.obj || [];
                var voList = new Array(list.length);
                if(list.length>0){
                    var finish = 0;
                    for(var i=0;i<list.length;i++){
                        (function(i){
                            knowledgeGroupDAO.list({parentId:list[i].rowId},function(err,$result){
                                var vo = convertVO(list[i]);
                                var $children = $result&&$result.obj || [];
                                for(var j=0;j<$children.length;j++){
                                    vo.children.push(convertVO($children[j]));
                                }
                                voList[i] = vo;
                                if(++finish == list.length){
                                    result.obj = voList;
                                    cb(null,result);
                                }
                            });
                        })(i);
                    }
                }else{
                    cb(null,result);
                }
            }
        ],function(err, result){
            next(err || result);
        });

        function convertVO(po){
            return {id:po.rowId,name:po.name,parentId:po.parentId,children:[],centers:po.centers};
        }
    },
};
